﻿using System;

namespace _1290_convert_binary_number_in_a_linked_list_to_integer
{


    public class ListNode
    {
        public int val;
        public ListNode next;
        public ListNode (int x) { val = x; }
    }

    class Program
    {
        static void Main (string[] args)
        {
            ListNode head = new ListNode (1);
            head.next = new ListNode (0);
            head.next.next = new ListNode (1);
            head.next.next.next = null;

            Console.WriteLine (GetDecimalValue(head));
        }

        public static int GetDecimalValue (ListNode head)
        {
            int sum = 0;
            int count = 1;
            ListNode node = head;

            while (node.next != null)
            {
                count *= 2;
                node = node.next;
            }

            while(head != null)
            {
                sum += head.val * count;
                count /= 2;
                head = head.next;
            }
            return sum;
        }
    }
}
